-- UIDungeonBuyLife
-- Created by panyl Mar/24/2015
-- 购买体力界面

UIDungeonBuyLife = class("UIDungeonBuyLife", function()
    return cc.Layer:create();
end);

function UIDungeonBuyLife.create(needCancel)
    return UIDungeonBuyLife.new(needCancel);
end

-- 构造函数
function UIDungeonBuyLife:ctor(needCancel)
    self:setName("UIDungeonBuyLife");

    self.needCancel = needCancel;

    local goodsId = MarketM.getGoodsIdByAlias("buy_life");
    self.goodsId = goodsId;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    self:init();
end

-- 注册事件处理回调函数
function UIDungeonBuyLife:registerEventCallback()
    -- 注册购买商品的事件回调
    EventMgr.register("UIDungeonBuyLife", event.BUY_GOODS, function(args)
        -- 提示玩家购买了商品
        local goodsId = args["goodsId"];

        if goodsId ~= self.goodsId then
            return;
        end

        if MarketM.query(goodsId, "classify") ~= "hero" then
            alert(string.format(getLocStr("market_buy_succ"), MarketM.query(goodsId, "name")));
        end

        -- 自动关闭购买界面
        UIMgr.getCurrentScene():removeForm(self);
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDungeonBuyLife 界面析构清理");

            EventMgr.removeAll("UIDungeonBuyLife");
        end
    end);
end

-- 初始化
function UIDungeonBuyLife:init()
    local goodsId = self.goodsId;
    local goodsInfo = MarketM.query(goodsId);
    local desc = goodsInfo["confirm"];

    if string.match(desc, "{goods}") ~= nil then
        -- 动态显示商品名称
        desc = string.gsub(desc, "{goods}", goodsInfo["name"]);
    end

    if string.match(desc, "{gem}") ~= nil then
        -- 动态显示钻石价格
        desc = string.gsub(desc, "{gem}", goodsInfo["price"]["gem"]);
    end

    if string.match(desc, "{life}") ~= nil then
        -- 动态显示体力
        desc = string.gsub(desc, "{added_life}", tostring(math.min(9, ME.user.dbase:query("life", 0) + ME.user:getMaxLife())))
        desc = string.gsub(desc, "{life}", tostring(ME.user.dbase:query("life", 0)));
        desc = string.gsub(desc, "{max_life}", tostring(ME.user:getMaxLife()));
    end

    -- 尝试购买
    local function tryBuy()
        local ret = MarketM.canBuyGoods(goodsId)
        if true ~= ret or type(ret) == "string" then
            if "gem_lack" == ret then
                -- 钻石不足
                showGemLackConfirm();
            elseif type(ret) == "string" then
                -- 有提示语，提示之
                alert(ret);
            else
                -- 其他原因，统一提示失败
                alert(getLocStr("market_buy_fail"));
            end

            -- 自动关闭购买界面
            UIMgr.getCurrentScene():removeForm(self);

            return;
        end

        if DungeonM.isInDungeon() and not DungeonM.isPause() then
            DungeonActionM.go("buy_market_goods", 1, goodsId);
        else
            -- 购买
            Operation.cmd_buy_goods(goodsId);
        end
    end

    -- 点击取消或返回时，需要关闭体力购买界面
    local function cancelFunc()
        closeFormByName("UIDungeonBuyLife");
    end

    if self.needCancel then
        confirm2(CONFIRM_TYPE_OK_CANCEL, getLocStr("life_not_enough"), desc,
            cancelFunc, tryBuy, getLocStr("btn_text_cancel"), getLocStr("btn_text_buy"),
            { ["okColor"] = "blue", ["backFunc"] = cancelFunc, });
    else
        confirm2(CONFIRM_TYPE_OK, getLocStr("life_not_enough"), desc,
            tryBuy, cancelFunc, getLocStr("btn_text_buy"), nil,
            { ["backFunc"] = cancelFunc, });
    end
end
